package org.huntaway.lucene.base;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.huntaway.kernel.NutException;
import org.huntaway.lucene.bean.LuceneBean;
import org.huntaway.lucene.demo.util.DocFilter;
import org.huntaway.lucene.demo.util.LuceneUtil;
import org.huntaway.lucene.document.impl.DirDocument;
import org.huntaway.lucene.document.impl.FileDocument;
import org.huntaway.lucene.document.impl.JavaDocument;
import org.huntaway.lucene.document.impl.TextDocument;
import org.huntaway.lucene.document.impl.XmlDocument;

public class BaseIndex {
	LuceneUtil luceneUtil = new LuceneUtil();
	IndexWriter writer = null;
	IndexWriter dirWriter = null;
	IndexWriter textWriter = null;
	IndexWriter javaWriter = null;
	IndexWriter xmlWriter = null;

	List<IndexWriter> indexWriterLst = null;
	// static final String INDEX_DIR = "I:/jskyme/org.apache.lucene/huntaway/";
	public static String INDEX_DIR = "";
	protected static List<String> docsLst = new ArrayList<String>();

	protected BaseIndex() {

	}

	protected void indexRootDocs(File file) throws IOException, NutException {
		writer = luceneUtil.buildWriter(INDEX_DIR + "defualt",true);
		dirWriter = luceneUtil.buildWriter(INDEX_DIR + "dir",false);
		textWriter = luceneUtil.buildWriter(INDEX_DIR + "text",false);
		javaWriter = luceneUtil.buildWriter(INDEX_DIR + "java",false);
		xmlWriter = luceneUtil.buildWriter(INDEX_DIR + "xml",false);

		indexDocs(file);

		writer.close();
		dirWriter.close();
		textWriter.close();
		javaWriter.close();
		xmlWriter.close();
	}

	protected void indexRootRawDocs(File file) throws IOException, NutException {
		LuceneBean Lucene = new LuceneBean("defualt", true);
		luceneUtil.buildWriter(Lucene,true);
		LuceneBean LuceneDir = new LuceneBean("dir", true);
		luceneUtil.buildWriter(LuceneDir,false);
		LuceneBean LuceneText = new LuceneBean("text", true);
		luceneUtil.buildWriter(LuceneText,false);
		LuceneBean LuceneJava = new LuceneBean("java", true);
		luceneUtil.buildWriter(LuceneJava,false);
		LuceneBean LuceneXml = new LuceneBean("xml", true);
		luceneUtil.buildWriter(LuceneXml,false);

		Directory dir1 = luceneUtil.buildRawDirectory();
		Directory dir2 = luceneUtil.buildRawDirectory();
		Directory dir3 = luceneUtil.buildRawDirectory();
		Directory dir4 = luceneUtil.buildRawDirectory();
		Directory dir5 = luceneUtil.buildRawDirectory();
		writer = luceneUtil.buildWriter(dir1,true);
		dirWriter = luceneUtil.buildWriter(dir2,false);
		textWriter = luceneUtil.buildWriter(dir3,false);
		javaWriter = luceneUtil.buildWriter(dir4,false);
		xmlWriter = luceneUtil.buildWriter(dir5,false);

		indexDocs(file);

		writer.close();
		dirWriter.close();
		textWriter.close();
		javaWriter.close();
		xmlWriter.close();

		writer = null;
		dirWriter = null;
		textWriter = null;
		javaWriter = null;
		xmlWriter = null;

		writer = luceneUtil.buildWriter(INDEX_DIR + "defualt",true);
		dirWriter = luceneUtil.buildWriter(INDEX_DIR + "dir",false);
		textWriter = luceneUtil.buildWriter(INDEX_DIR + "text",false);
		javaWriter = luceneUtil.buildWriter(INDEX_DIR + "java",false);
		xmlWriter = luceneUtil.buildWriter(INDEX_DIR + "xml",false);
		writer.addIndexes(dir1);
		dirWriter.addIndexes(dir2);
		textWriter.addIndexes(dir3);
		javaWriter.addIndexes(dir4);
		xmlWriter.addIndexes(dir5);

		writer.close();
		dirWriter.close();
		textWriter.close();
		javaWriter.close();
		xmlWriter.close();
	}

	protected void addDocument(File file) throws CorruptIndexException,
			IOException, NutException {
		if (DocFilter.filterFile(file)) {
			logln("ignore " + file);
			return;
		}
		logln("adding " + file);
		if (file.getName().endsWith(".text")) {
			textWriter.addDocument(new TextDocument().document(file));
		}
		if (file.getName().endsWith(".java")) {
			javaWriter.addDocument(new JavaDocument().document(file));
		}
		if (file.getName().endsWith(".xml")) {
			xmlWriter.addDocument(new XmlDocument().document(file));
		}
		writer.addDocument(new FileDocument().document(file));
		// throw new NutException("This code is not be run.....");
	}

	protected void addDirDocument(File file) throws CorruptIndexException,
			IOException, NutException {
		if (DocFilter.filterFile(file)) {
			logln("ignore " + file);
			return;
		}
		dirWriter.addDocument(new DirDocument().document(file));
		// throw new NutException("This code is not be run.....");
	}

	protected void indexDocs(File file) throws IOException, NutException {
		if (file.canRead()) {
			if (file.isDirectory()) {
				addDirDocument(file);
				if (DocFilter.filterDir(file)) {
					System.out.println("ignore " + file);
					return;
				}
				File[] files = file.listFiles();
				if (files != null) {
					for (int i = 0; i < files.length; i++) {
						indexDocs(files[i]);
					}
				}
			} else {
				addDocument(file);
			}
		}
	}

	// recurrence recursion
	protected void logln(String str) {
		System.out.println(str);
	}

	// ---------------------------------------------------------------------------------------
	protected void indexDocByRaw(File file,boolean create) throws IOException, NutException {	
		luceneUtil.setWriter(Lucene,create);
		indexDocsByRaw(Lucene,file,create);
		luceneUtil.closeWriter(Lucene,create);
	}
	LuceneBean Lucene = new LuceneBean("defualt", true);
	int docNum=0;
	int indexNum=0;
	protected void indexDocsByRaw(LuceneBean lucene,File file, boolean create) throws IOException,
			NutException {
		if (file.canRead()) {
			if(DocFilter.isFilter(file)){
				return;
			}
			if (file.isDirectory()) {
				//addDirDocument(Lucene, file);
				if (DocFilter.filterDir(file)) {
					System.out.println("ignore " + file);
					return;
				}
				File[] files = file.listFiles();
				if (files != null) {
					for (int i = 0; i < files.length; i++) {
						indexDocsByRaw(lucene,files[i],create);
					}
				}
			} else {
				docNum++;
				addDocumentByRaw(file);
				if(docNum==5000){
					docNum=0;
					indexNum++;
					luceneUtil.commitWriter(lucene,create);
					create=false;
				}
			}
		}
	}
	DirDocument dirDocument=new DirDocument();
	TextDocument textDocument=new TextDocument();
	JavaDocument javaDocument=new JavaDocument();
	XmlDocument xmlDocument=new XmlDocument();
	FileDocument fileDocument=new FileDocument();
	protected void addDirDocument(LuceneBean Lucene, File file)
			throws CorruptIndexException, IOException, NutException {
		if (DocFilter.filterDir(file)) {
			logln("ignore " + file);
			return;
		}
		dirDocument.addDocument(luceneUtil.getIndexWriter(Lucene), file);
	}

	protected void addDocumentByRaw(File file)
			throws CorruptIndexException, IOException, NutException {
		if (DocFilter.filterFile(file)) {
			logln("ignore " + file);
			return;
		}
		logln("adding " + file);
		if (file.getName().endsWith(".text")) {
			textDocument.addDocument(luceneUtil.getIndexWriter(Lucene),
					file);
		} else if (file.getName().endsWith(".java")) {
			javaDocument.addDocument(luceneUtil.getIndexWriter(Lucene),
					file);
		} else if (file.getName().endsWith(".xml")) {
			xmlDocument.addDocument(luceneUtil.getIndexWriter(Lucene),
					file);
		} else {
			fileDocument.addDocument(luceneUtil.getIndexWriter(Lucene),
					file);
		}
	}
}
